package com.sentiment.tools;

import java.util.Date;

import org.bson.Document;
import org.bson.types.ObjectId;

import com.sentiment.database.bean.Keyword;
import com.sentiment.database.dbUtil.DbUtil;

public class KeywordUtil {
	
	/**
	 * 给一个关键词，搜索数据库中是否已有该关键词的查找记录
	 * 返回关键词的id
	 */
	public static String findAndInsert(String keywd){
		DbUtil dbu = new DbUtil();
		dbu.openConnection(Keyword.dbName, Keyword.collection);
		Keyword kwd = new Keyword(new Date(), keywd, null);
		Document doc = dbu.query(kwd.toDocument(), new String[]{"keyword"});
		if(doc == null){
			ObjectId id = dbu.insertGetId(kwd.toDocument());
			return id.toString();
		}
		kwd = new Keyword(doc);
		dbu.closeConnection();
		return kwd.get_id();
	}
}
